#include "windownum1.h"
#include "ui_windownum1.h"
#include "ownerinfo.h"

WindowNum1::WindowNum1(QWidget *parent):
    QWidget(parent), ui(new Ui::WindowNum1)
{
    ui->setupUi(this);

    /*连接数据库*/
    mydatabase = QSqlDatabase::addDatabase("QMYSQL");
    mydatabase.setHostName("localhost");
    mydatabase.setUserName("root");
    mydatabase.setPassword("");
    mydatabase.setDatabaseName("petmanage");
    if(mydatabase.open())
    {
        QMessageBox::information(this, "连接提示", "数据库连接成功啦！٩(◕‿◕｡)۶");
    }
    else
    {
        QMessageBox::warning(this, "连接提示", "数据库连接失败啦！(▼皿▼#)");
    }

    /*样式表 让按键显示照片*/
    QPixmap Logo("C:\\Users\\26057\\Desktop\\Qt code\\Test 002\\sample002\\logo.jpg");
    ui->LogoPicture->setIcon(QIcon(Logo));
    ui->LogoPicture->setIconSize(QSize(70, 70));

    QPixmap Headportrait("C:\\Users\\26057\\Desktop\\Qt code\\Test 002\\sample002\\Headportrait.jpg");
    ui->HeadportraitPicture->setIcon(QIcon(Headportrait));
    ui->HeadportraitPicture->setIconSize(QSize(70, 70));

    QPixmap vaccine("C:\\Users\\26057\\Desktop\\Qt code\\Test 002\\sample002\\vaccine.jpg");
    ui->VaccineButton->setIcon(QIcon(vaccine));
    ui->VaccineButton->setIconSize(QSize(70, 70));

    QPixmap medicine("C:\\Users\\26057\\Desktop\\Qt code\\Test 002\\sample002\\medicine.jpg");
    ui->MedicineButton->setIcon(QIcon(medicine));
    ui->MedicineButton->setIconSize(QSize(70, 70));
}

WindowNum1::~WindowNum1()
{
    delete ui;
}

/*录入宠物信息button*/
void WindowNum1::on_TypeinButton_clicked()
{    
    /*获取输入框内容*/
    QString PetID = ui->PetIDInputbox->text();
    QString PetName = ui->PetNameInputbox->text();
    QString PetCategory = ui->PetCategoryInputbox->text();
    QString PetSex = ui->PetSexInputbox->text();
    QString PetBirthday = ui->PetBirthdayInputbox->text();
    QString OwnerId = ui->OwnerIdInputbox->text();

    /*发送之前检测信息输入是否完整*/
    if (PetID.isEmpty()
        ||PetName.isEmpty()
        ||PetCategory.isEmpty()
        ||PetSex.isEmpty()
        ||PetBirthday.isEmpty()
        ||OwnerId.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请完整输入信息！");
        return;
    }

    /*合并输入框内容准备插入*/
    QString Insert = QString("insert into PetInfo values('%1','%2','%3','%4','%5','%6');").arg(PetID).arg(PetName).arg(PetCategory).arg(PetSex).arg(PetBirthday).arg(OwnerId);

    /*插入表 并提示是否判断成功*/
    QSqlQuery Query;
    if(Query.exec(Insert))
    {
        QMessageBox::information(this, "插入提示", "插入数据成功！( ﾟ∀ﾟ)ｱﾊﾊ八八ﾉヽﾉヽﾉヽﾉ ＼ / ＼");
        /*插入数据成功后清空输入框里面的内容*/
        ui->PetIDInputbox->clear();
        ui->PetNameInputbox->clear();
        ui->PetCategoryInputbox->clear();
        ui->PetSexInputbox->clear();
        ui->PetBirthdayInputbox->clear();
        ui->OwnerIdInputbox->clear();
    }
    else
    {
        QMessageBox::warning(this, "插入提示", "插入数据失败！");
    }
}


void WindowNum1::on_SearchButton_clicked()
{
    /*在tableView中显示查找到的数据*/
    Model = new QSqlTableModel;
    Model->setTable("petinfo");
    ui->tableView->setModel(Model);
    Model->select();
}


void WindowNum1::on_DeleteByPetID_clicked()
{
    QString PetID = ui->PetIDInputbox->text();
    QString DeleteSQL = QString("DELETE FROM PetInfo WHERE PetID=%1").arg(PetID);
    QSqlQuery Query;
    if(Query.exec(DeleteSQL))
    {
        QMessageBox::information(this, "删除提示", "删除数据成功！");
        ui->PetIDInputbox->clear();
    }
    else
    {
        QMessageBox::warning(this, "删除提示", "删除数据失败！");
    }
}


void WindowNum1::on_DeleteByPetName_clicked()
{
    QString PetName = ui->PetNameInputbox->text();
    QString DeleteSQL = QString("DELETE FROM PetInfo WHERE PetName='%1'").arg(PetName);
    QSqlQuery Query;
    if(Query.exec(DeleteSQL))
    {
        QMessageBox::information(this, "删除提示", "删除数据成功！");
        ui->PetNameInputbox->clear();
    }
    else
    {
        QMessageBox::warning(this, "删除提示", "删除数据失败！");
    }
}


void WindowNum1::on_DeleteByPetCategory_clicked()
{
    QString PetCategory = ui->PetCategoryInputbox->text();
    QString DeleteSQL = QString("DELETE FROM PetInfo WHERE PetCategory='%1'").arg(PetCategory);
    QSqlQuery Query;
    if(Query.exec(DeleteSQL))
    {
        QMessageBox::information(this, "删除提示", "删除数据成功！");
        ui->PetCategoryInputbox->clear();
    }
    else
    {
        QMessageBox::warning(this, "删除提示", "删除数据失败！");
    }
}


void WindowNum1::on_DeleteByPetSex_clicked()
{
    QString PetSex = ui->PetSexInputbox->text();
    QString DeleteSQL = QString("DELETE FROM PetInfo WHERE PetSex='%1'").arg(PetSex);
    QSqlQuery Query;
    if(Query.exec(DeleteSQL))
    {
        QMessageBox::information(this, "删除提示", "删除数据成功！");
        ui->PetSexInputbox->clear();
    }
    else
    {
        QMessageBox::warning(this, "删除提示", "删除数据失败！");
    }
}


void WindowNum1::on_DeleteByPetBirthday_clicked()
{
    QString PetBirthday = ui->PetBirthdayInputbox->text();
    QString DeleteSQL = QString("DELETE FROM PetInfo WHERE PetBirthday='%1'").arg(PetBirthday);
    QSqlQuery Query;
    if(Query.exec(DeleteSQL))
    {
        QMessageBox::information(this, "删除提示", "删除数据成功！");
        ui->PetBirthdayInputbox->clear();
    }
    else
    {
        QMessageBox::warning(this, "删除提示", "删除数据失败！");
    }
}


void WindowNum1::on_DeleteByOwnerID_clicked()
{
    QString OwnerID = ui->OwnerIdInputbox->text();
    QString DeleteSQL = QString("DELETE FROM PetInfo WHERE OwnerID=%1").arg(OwnerID);
    QSqlQuery Query;
    if(Query.exec(DeleteSQL))
    {
        QMessageBox::information(this, "删除提示", "删除数据成功！");
        ui->OwnerIdInputbox->clear();
    }
    else
    {
        QMessageBox::warning(this, "删除提示", "删除数据失败！");
    }
}


void WindowNum1::on_FindByPetID_clicked()
{
    /*创建PetID获取文本框中内容*/
    QString PetID = ui->PetIDInputbox->text();

    /*判断PetID是否正常输入*/
    if (PetID.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入要查询的PetID！");
        return;
    }

    /*创建数据模型*/
    Model = new QSqlTableModel;
    /*根据PetID查询表*/
    Model->setQuery(QString("SELECT * FROM PetInfo WHERE PetID = %1").arg(PetID));

    /*检查是否有结果*/
    if (Model->rowCount() == 0) {
        QMessageBox::information(this, "查询结果", "未找到该PetID对应的记录！");
        ui->PetIDInputbox->clear();
        delete Model;
        return;
    }

    ui->tableView->setModel(Model);

    /*查询成功后清除文本框输入内容*/
    ui->PetIDInputbox->clear();
}


void WindowNum1::on_FindByPetName_clicked()
{
    /*创建PetName获取文本框中内容*/
    QString PetName = ui->PetNameInputbox->text();

    /*判断PetName是否正常输入*/
    if (PetName.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入要查询的PetName！");
        return;
    }

    /*创建数据模型*/
    Model = new QSqlTableModel;
    /*根据PetName查询表*/
    Model->setQuery(QString("SELECT * FROM PetInfo WHERE PetName = '%1'").arg(PetName));

    /*检查是否有结果*/
    if (Model->rowCount() == 0) {
        QMessageBox::information(this, "查询结果", "未找到该PetName对应的记录！");
        ui->PetNameInputbox->clear();
        delete Model;
        return;
    }

    /*数据模型放入tableView*/
    ui->tableView->setModel(Model);

    /*查询成功后清除文本框输入内容*/
    ui->PetNameInputbox->clear();
}


void WindowNum1::on_FindByPetCategory_clicked()
{
    /*创建PetCategory获取文本框中内容*/
    QString PetCategory = ui->PetCategoryInputbox->text();

    /*判断PetCategory是否正常输入*/
    if (PetCategory.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入要查询的PetCategory！");
        return;
    }

    /*创建数据模型*/
    Model = new QSqlTableModel;

    /*根据PetCategory查询表*/
    Model->setQuery(QString("SELECT * FROM PetInfo WHERE PetCategory = '%1'").arg(PetCategory));

    /*检查是否有结果*/
    if (Model->rowCount() == 0) {
        QMessageBox::information(this, "查询结果", "未找到该PetCategory对应的记录！");
        ui->PetCategoryInputbox->clear();
        delete Model;
        return;
    }

    /*数据模型放入tableView*/
    ui->tableView->setModel(Model);

    /*查询成功后清除文本框输入内容*/
    ui->PetCategoryInputbox->clear();
}


void WindowNum1::on_FindByPetSex_clicked()
{
    /*创建PetSex获取文本框中内容*/
    QString PetSex = ui->PetSexInputbox->text();

    /*判断PetSex是否正常输入*/
    if (PetSex.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入要查询的PetSex！");
        return;
    }

    /*创建数据模型*/
    Model = new QSqlTableModel;

    /*根据PetSex查询表*/
    Model->setQuery(QString("SELECT * FROM PetInfo WHERE PetSex = '%1'").arg(PetSex));

    /*检查是否有结果*/
    if (Model->rowCount() == 0) {
        QMessageBox::information(this, "查询结果", "未找到该PetSex对应的记录！");
        ui->PetSexInputbox->clear();
        delete Model;
        return;
    }

    /*数据模型放入tableView*/
    ui->tableView->setModel(Model);

    /*查询成功后清除文本框输入内容*/
    ui->PetSexInputbox->clear();
}


void WindowNum1::on_FindByPetBirthday_clicked()
{
    /*创建PetBirthday获取文本框中内容*/
    QString PetBirthday = ui->PetBirthdayInputbox->text();

    /*判断PetBirthday是否正常输入*/
    if (PetBirthday.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入要查询的PetHobby！");
        return;
    }

    /*创建数据模型*/
    Model = new QSqlTableModel;

    /*根据PetBirthday查询表*/
    Model->setQuery(QString("SELECT * FROM PetInfo WHERE PetHobby = '%1'").arg(PetBirthday));

    /*检查是否有结果*/
    if (Model->rowCount() == 0) {
        QMessageBox::information(this, "查询结果", "未找到该PetHobby对应的记录！");
        ui->PetBirthdayInputbox->clear();
        delete Model;
        return;
    }

    /*数据模型放入tableView*/
    ui->tableView->setModel(Model);

    /*查询成功后清除文本框输入内容*/
    ui->PetBirthdayInputbox->clear();
}


void WindowNum1::on_FindByOwnerID_clicked()
{
    /*创建OwnerID获取文本框中内容*/
    QString OwnerID = ui->OwnerIdInputbox->text();

    /*判断OwnerID是否正常输入*/
    if (OwnerID.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入要查询的OwnerID！");
        return;
    }

    /*创建数据模型*/
    Model = new QSqlTableModel;

    /*根据OwnerID查询表*/
    Model->setQuery(QString("SELECT * FROM PetInfo WHERE OwnerID = '%1'").arg(OwnerID));

    /*检查是否有结果*/
    if (Model->rowCount() == 0) {
        QMessageBox::information(this, "查询结果", "未找到该OwnerID对应的记录！");
        ui->PetBirthdayInputbox->clear();
        delete Model;
        return;
    }

    /*数据模型放入tableView*/
    ui->tableView->setModel(Model);

    /*查询成功后清除文本框输入内容*/
    ui->OwnerIdInputbox->clear();
}


void WindowNum1::on_ChangeByPetID_clicked()
{
    /*获取修改行对应的ID*/
    QString PetID = ui->PetIDInputboxKey1->text();

    /*获取新ID值*/
    QString NewPetID = ui->PetIDNew->text();

    /*验证数据输入是否完整*/
    if(PetID.isEmpty() || NewPetID.isEmpty()){
        QMessageBox::warning(this, "输入错误", "所有字段必须填写完整！");
        return;
    }

    /*修改语句*/
    QString Update = QString("UPDATE PetInfo SET PetID=%1 WHERE PetID=%2;").arg(NewPetID).arg(PetID);

    QSqlQuery Query;
    if(Query.exec(Update)){
        QMessageBox::information(this, "更新成功", "恭喜你！更新成功！");
        ui->PetIDInputboxKey1->clear();
        ui->PetIDNew->clear();
    }
    else
    {
        QMessageBox::information(this, "更新失败", "很遗憾！更新失败！");
    }
}


void WindowNum1::on_ChangeByPetName_clicked()
{
    /*获取修改行对应的ID*/
    QString PetID = ui->PetIDInputboxKey2->text();

    /*获取新ID值*/
    QString NewPetName = ui->PetNameNew->text();

    /*验证数据输入是否完整*/
    if(PetID.isEmpty() || NewPetName.isEmpty()){
        QMessageBox::warning(this, "输入错误", "所有字段必须填写完整！");
        return;
    }

    /*修改语句*/
    QString Update = QString("UPDATE PetInfo SET PetName='%1' WHERE PetID=%2;").arg(NewPetName).arg(PetID);

    QSqlQuery Query;
    if(Query.exec(Update)){
        QMessageBox::information(this, "更新成功", "恭喜你！更新成功！");
        ui->PetIDInputboxKey2->clear();
        ui->PetNameNew->clear();
    }
    else
    {
        QMessageBox::information(this, "更新失败", "很遗憾！更新失败！");
    }
}



void WindowNum1::on_ChangeByPetCategory_clicked()
{
    /*获取修改行对应的ID*/
    QString PetID = ui->PetIDInputboxKey3->text();

    /*获取新ID值*/
    QString NewPetCategory = ui->PetCategoryNew->text();

    /*验证数据输入是否完整*/
    if(PetID.isEmpty() || NewPetCategory.isEmpty()){
        QMessageBox::warning(this, "输入错误", "所有字段必须填写完整！");
        return;
    }

    /*修改语句*/
    QString Update = QString("UPDATE PetInfo SET PetCategory='%1' WHERE PetID=%2;").arg(NewPetCategory).arg(PetID);

    QSqlQuery Query;
    if(Query.exec(Update)){
        QMessageBox::information(this, "更新成功", "恭喜你！更新成功！");
        ui->PetIDInputboxKey3->clear();
        ui->PetCategoryNew->clear();
    }
    else
    {
        QMessageBox::information(this, "更新失败", "很遗憾！更新失败！");
    }
}


void WindowNum1::on_ChangeByPetSex_clicked()
{
    /*获取修改行对应的ID*/
    QString PetID = ui->PetIDInputboxKey4->text();

    /*获取新ID值*/
    QString NewPetSex = ui->PetSexNew->text();

    /*验证数据输入是否完整*/
    if(PetID.isEmpty() || NewPetSex.isEmpty()){
        QMessageBox::warning(this, "输入错误", "所有字段必须填写完整！");
        return;
    }

    /*修改语句*/
    QString Update = QString("UPDATE PetInfo SET PetSex='%1' WHERE PetID=%2;").arg(NewPetSex).arg(PetID);

    QSqlQuery Query;
    if(Query.exec(Update)){
        QMessageBox::information(this, "更新成功", "恭喜你！更新成功！");
        ui->PetIDInputboxKey4->clear();
        ui->PetSexNew->clear();
    }
    else
    {
        QMessageBox::information(this, "更新失败", "很遗憾！更新失败！");
    }
}


void WindowNum1::on_ChangeByPetBirthday_clicked()
{
    /*获取修改行对应的ID*/
    QString PetID = ui->PetIDInputboxKey5->text();

    /*获取新ID值*/
    QString NewPetBirthday = ui->PetBirthdayNew->text();

    /*验证数据输入是否完整*/
    if(PetID.isEmpty() || NewPetBirthday.isEmpty()){
        QMessageBox::warning(this, "输入错误", "所有字段必须填写完整！");
        return;
    }

    /*修改语句*/
    QString Update = QString("UPDATE PetInfo SET PetBirthday='%1' WHERE PetID=%2;").arg(NewPetBirthday).arg(PetID);

    QSqlQuery Query;
    if(Query.exec(Update)){
        QMessageBox::information(this, "更新成功", "恭喜你！更新成功！");
        ui->PetIDInputboxKey5->clear();
        ui->PetBirthdayNew->clear();
    }
    else
    {
        QMessageBox::information(this, "更新失败", "很遗憾！更新失败！");
    }
}


void WindowNum1::on_ChangeByOwnerID_clicked()
{
    /*获取修改行对应的ID*/
    QString PetID = ui->PetIDInputboxKey6->text();

    /*获取新ID值*/
    QString NewOwnerID = ui->OwnerIDNew->text();

    /*验证数据输入是否完整*/
    if(PetID.isEmpty() || NewOwnerID.isEmpty()){
        QMessageBox::warning(this, "输入错误", "所有字段必须填写完整！");
        return;
    }

    /*修改语句*/
    QString Update = QString("UPDATE PetInfo SET OwnerID=%1 WHERE PetID=%2;").arg(NewOwnerID).arg(PetID);

    QSqlQuery Query;
    if(Query.exec(Update)){
        QMessageBox::information(this, "更新成功", "恭喜你！更新成功！");
        ui->PetIDInputboxKey6->clear();
        ui->OwnerIDNew->clear();
    }
    else
    {
        QMessageBox::information(this, "更新失败", "很遗憾！更新失败！");
    }
}


void WindowNum1::on_HeadportraitPicture_clicked()
{
    OwnerInfo *ChangeOwnerInfo = new OwnerInfo;
    ChangeOwnerInfo->show();
}


void WindowNum1::on_MedicineButton_clicked()
{
    QDesktopServices::openUrl(QUrl("https://s.taobao.com/search?page=1&q=%E5%AE%A0%E7%89%A9%E8%8D%AF%E5%93%81&spm=a21bo.jianhua%2Fa.201867-main.d10_fourth.5af92a89ulirt6&tab=all"));
}


void WindowNum1::on_VaccineButton_clicked()
{
    QDesktopServices::openUrl(QUrl("file:///C:/Users/26057/Desktop/Qt code/宠物疫苗预约平台.html"));
}


